package ch.gibb.learnit.client.controllers;

import ch.gibb.learnit.client.util.soap.User;
import org.apache.log4j.Logger;

/**
 * Controller for the Login and Register page
 *
 * @author Bernard Jaquet
 * @version 1.0
 */
public class LoginAction extends MasterAction {

    // IN
    private String login;
    private String register;
    private String firstname;
    private String surname;
    private String mail;
    private String password;
    private String repeadPassword;

    //Class
    private final Logger masterLogger = Logger.getLogger(LoginAction.class);

    @Override
    public int openPage() {
        //Sets the title of the Page
        super.title = "Willkommen bei LearnIT";

        // Logic for tje login
        if (login != null && !login.equals("")) {
            masterLogger.debug("logging in user");
            try {
                User user = factory.createUser();
                user.setEmail(factory.createUserEmail(mail));
                user.setPw(factory.createUserPw(password));
                int userId = client.login(user);
                if (userId == -1) {
                    getErrors().addMessage("Benutzername oder Passwort falsch");
                } else {
                    loginUser(userId, password);
                    return redirect("home");
                }
            } catch (Exception e) {
                getErrors().addMessage("Ein unerwarteter Fehler ist aufgetreten");
                masterLogger.error("Error while login user " + e.getMessage(), e);
            }
        }

        // Logic for the registration
        if (register != null && !register.equals("")) {
            masterLogger.debug("registering user");
            try{
                if(!password.equals(repeadPassword)){
                    masterLogger.debug(password + " = " + repeadPassword);
                    getErrors().addMessage("Bitte zwei mal das selbe Passwort eingeben");
                    return 0;
                }
                User user = factory.createUser();
                user.setEmail(factory.createUserEmail(mail));
                user.setVorname(factory.createUserVorname(firstname));
                user.setNachname(factory.createUserNachname(surname));
                user.setPw(factory.createUserPw(password));
                int userId = client.addUser(user);
                if(userId == -1){
                    getErrors().addMessage("Es ist ein Fehler während der Registrierung aufgetreten");
                } else {
                    loginUser(userId, password);
                    redirect("profile");
                }
                masterLogger.info("Registered User " + user.getEmail());
            } catch (Exception e) {
                getErrors().addMessage("Ein unerwarteter Fehler ist aufgetreten");
                masterLogger.error("Error while registering user " + e.getMessage(), e);
            }
        }

        return 0;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getRegister() {
        return register;
    }

    public void setRegister(String register) {
        this.register = register;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRepeadPassword() {
        return repeadPassword;
    }

    public void setRepeadPassword(String repeadPassword) {
        this.repeadPassword = repeadPassword;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }
}
